(function(win){
	var Z = win.Z,
		bundle = new Z.Bundle({'tmpl' : 'document'});

	bundle.addEvent('a.add', 'click', function(){
		form.clear().open();
		return false;
	});
	
	bundle.addEvent('a.search', 'click', function(){

		return false;
	});

	bundle.addEvent('thead a', 'click', function(){
		var field = $(this).data('field'),
		direction = (typeof $(this).data('direction') == 'undefined') ? 1 : $(this).data('direction');
		$(this).data('direction', direction * -1);
		loader.load({'orderField' : field, 'orderDirection' : direction});
		return false;
	});

	bundle.addEvent('tbody', 'click', 'a.edit', function(){
		var id = $(this).parent().siblings().first().find(':checkbox').val();
		Z.ajax({
			'url' : Z.route('document','find'),
			'data' : {'id' : id},
			'success' : function(data){
				form.open().fill(data);
			}
		});
		return false;
	});

	bundle.onload = function(){
		form = new Z.Form({
			'title' : '添加/修改',
			'content' : bundle.find('script.form-tmpl').html(),
			'width' : 800,
			'action' : Z.route('document','set')
		});	
		
		form.onsubmit = form.onsubmit = function(){
			loader.reload();
		};
		
		loader = new Z.Loader({
			'url': Z.route('document', 'records'),
			'listTmpl': bundle.find('script.list-tmpl').html(),
			'list': bundle.find('tbody'),
			'pagination': bundle.find('tfoot td')
		});

		loader.load({'pageSize' : 20});

	};

	Z.BundleList.append(bundle);
})(window);